/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/

#include "StdAfx.h"
#include "PGLISAPIRenderer.h"


UINT CPGLISAPIRenderer::m_stuMaxWidth=640;
UINT CPGLISAPIRenderer::m_stuMaxHeight=480;

void CPGLISAPIRenderer::WriteRegion()
{
	switch(m_eOutputType)
	{
	case OutputPNG:
	case OutputJPG:
	case OutputBMP:
		WriteRegionAsImage();
		break;
	case OutputSVG:
		WriteRegionAsSVG();
	}
}


void CPGLISAPIRenderer::WriteRegionAsImage()
{
	if (!m_pServer || !m_pCtxt || !m_pImage || !m_pRegion)
		return;
	
	CString str,str2;
	DWORD dwBuffer=1024;

	// Creating DC...
	HDC hDC=::CreateDC("DISPLAY",NULL,NULL,NULL);
	ISAPIVERIFY(hDC);

	CPGLArchive ar(m_pRegion);

	str=_T("Generated by Plot Graphic Library for ");
	m_pCtxt->GetServerVariable(_T("REMOTE_HOST"), str2.GetBufferSetLength(dwBuffer), &dwBuffer);
	str2.ReleaseBuffer();
	str+=str2;
	str+=CTime::GetCurrentTime().Format(", %b-%m-%Y, %H:%M:%S");

	ar.SetTextOverlay( str );
	ar.ExportToImage(m_pImage, hDC, true);

	ISAPIVERIFY(::DeleteDC(hDC));

	WriteImage();
}

void CPGLISAPIRenderer::WriteRegionAsSVG()
{
	if (!m_pServer || !m_pCtxt || !m_pRegion)
		return;


	CString str,str2,strSVG;
	DWORD dwBuffer=1024;

	// Creating DC...
	HDC hDC=::CreateDC("DISPLAY",NULL,NULL,NULL);
	ISAPIVERIFY(hDC);

	CPGLArchive ar(m_pRegion);

	str=_T("Generated by Plot Graphic Library for ");
	m_pCtxt->GetServerVariable(_T("REMOTE_HOST"), str2.GetBufferSetLength(dwBuffer), &dwBuffer);
	str2.ReleaseBuffer();
	str+=str2;
	str+=CTime::GetCurrentTime().Format(", %b-%m-%Y, %H:%M:%S");

	ar.SetTextOverlay( str );
	ar.ExportToString(CPGLArchive::FileFormatSVG, strSVG, m_dim, hDC);

	ISAPIVERIFY(::DeleteDC(hDC));

/*
	// formulate a proper header
	m_pCtxt->m_bSendHeaders=FALSE;
	str.Format(_T("HTTP/1.0 200 OK\r\nimage/svg+xml\r\nContent-length: %d\r\n\r\n"), strSVG.GetLength()*sizeof(TCHAR));
	m_pServer->AddHeader(m_pCtxt, str);
*/
	// formulate a proper header
	*m_pCtxt<<_T("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n");
	*m_pCtxt<<strSVG;
}

void CPGLISAPIRenderer::SetOutputType( EOutputType eOutputType)
{
	m_eOutputType = eOutputType;

	switch(eOutputType)
	{
	case OutputPNG:
	case OutputJPG:
	case OutputBMP:
		if (m_pImage && (m_pImage->GetWidth() != m_dim.x || m_pImage->GetWidth() != m_dim.y))
		{
			delete m_pImage;
			m_pImage=NULL;
		}

		if (!m_pImage)
			m_pImage = new Gdiplus::Bitmap(m_dim.x, m_dim.y);
		break;
	case OutputSVG:
		if (m_pImage)
		{
			delete m_pImage;
			m_pImage=NULL;
		}
	}
}

void CPGLISAPIRenderer::SetDimensions( UINT uWidth, UINT uHeight)
{
	m_dim.x=__min( m_stuMaxWidth, uWidth);
	m_dim.y=__min( m_stuMaxHeight, uHeight);

	switch(m_eOutputType)
	{
	case OutputPNG:
	case OutputJPG:
	case OutputBMP:
		if (m_pImage && (m_pImage->GetWidth() != m_dim.x || m_pImage->GetWidth() != m_dim.y))
		{
			delete m_pImage;
			m_pImage=NULL;
		}

		if (!m_pImage)
			m_pImage = new Gdiplus::Bitmap(m_dim.x, m_dim.y);
	}
}
